<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">门户栏目列表</a></el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索、添加、自定义列 -->
      <el-row :gutter="20">
        <el-col :span="12"
          ><el-input placeholder="请输入栏目名称" v-model="query.name">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="showAddDialog"
            >新增门户栏目</el-button
          >
        </el-col>
        <el-col :span="3">
          <el-button type="success" @click="showSetColumn">自定义列</el-button>
        </el-col>
      </el-row>

      <!-- 内容列表 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '栏目名称').show"
          label="栏目名称"
          prop="name"
          width="250"
          :show-overflow-tooltip="true"
        ></el-table-column>

        <el-table-column
          v-if="showColumns.find((i) => i.label == '栏目编号').show"
          label="栏目编号"
          prop="id"
          width="100"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '栏目描述').show"
          label="栏目描述"
          prop="description"
          width="350"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '栏目排序').show"
          label="栏目排序"
          prop="sort"
          width="150"
        >
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '创建时间').show"
          label="创建时间"
          prop="createTime"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime.substring(0, 16) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '修改时间').show"
          label="修改时间"
          prop="modifyTime"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.modifyTime.substring(0, 16) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="showColumns.find((i) => i.label == '备注').show"
          label="备注"
          prop="remarks"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-tooltip effect="dark" content="查看" placement="top">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="showDetailDialog(scope.row)"
              ></el-button>
            </el-tooltip>
            <!-- 修改 -->
            <el-tooltip effect="dark" content="修改" placement="top">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row)"
              ></el-button>
            </el-tooltip>

            <!-- 删除按钮 -->
            <el-tooltip effect="dark" content="删除" placement="top">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="del(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 自定义列 -->
    <CustomColumn ref="columns" :showColumns="showColumns"></CustomColumn>

    <!-- 详情 -->
    <el-dialog
      title="门户栏目详情"
      :visible.sync="showDetailVisible"
      width="70%"
      @close="hideDetailDialog"
      center
    >
      <el-form :model="detail" label-width="80px">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="栏目名称:">
              <el-input v-model="detail.name" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="栏目编号:">
              <el-input v-model="detail.id" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="栏目排序:">
              <el-input v-model="detail.sort" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="创建时间:">
              <el-input v-model="detail.createTime" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="修改时间:">
              <el-input v-model="detail.modifyTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col>
            <el-form-item label="栏目描述:">
              <el-input
                type="textarea"
                v-model="editForm.description"
                rows="6"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col>
            <el-form-item label="备注:">
              <el-input
                type="textarea"
                v-model="detail.remarks"
                disabled
                rows="3"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="hideDetailDialog">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增 -->
    <el-dialog
      title="新增门户栏目"
      :visible.sync="showAddVisible"
      width="70%"
      @close="hideAddDialog"
    >
      <!-- 内容主体区域 -->
      <el-form :model="addForm" ref="addFormRef" label-width="100px">
        <el-row :gutter="30">
          <el-col :span="8"
            ><el-form-item
              label="栏目名称"
              prop="name"
              :rules="[
                {
                  required: true,
                  message: '栏目名称不能为空',
                  trigger: 'blur',
                },
                {
                  min: 1,
                  max: 12,
                  message: '长度须在1～12个字符之间',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input v-model="addForm.name"></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="栏目排序:" prop="sort">
              <el-input v-model="addForm.sort"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col
            ><el-form-item label="栏目描述" prop="description">
              <el-input
                type="textarea"
                v-model="addForm.description"
                rows="6"
                placeholder="暂无"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-form-item label="备注:">
          <el-input
            type="textarea"
            v-model="addForm.remarks"
            rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideAddDialog">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="门户栏目修改"
      :visible.sync="showEditVisible"
      width="70%"
      @close="hideEditDialog"
    >
      <el-form :model="editForm" ref="editFormRef" label-width="80px">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="栏目名称:" prop="name">
              <el-input v-model="editForm.name" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="栏目排序:" prop="sort">
              <el-input v-model="editForm.sort"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="栏目描述:" prop="description">
              <el-input
                type="textarea"
                v-model="editForm.description"
                rows="6"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注:" prop="remarks">
          <el-input
            type="textarea"
            v-model="editForm.remarks"
            rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideEditDialog">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import md5 from "js-md5";
import CustomColumn from "./components/CustomColumn.vue";
export default {
  components: { CustomColumn },
  data() {
    return {
      //获取用户列表的参数
      query: {
        name: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 内容列表
      list: [],
      // 内容总条数
      total: 0,
      // 初始化显示字段
      showColumns: [
        { label: "栏目名称", show: true },
        { label: "栏目描述", show: true },
        { label: "栏目编号", show: false },
        { label: "栏目排序", show: true },
        { label: "创建时间", show: false },
        { label: "修改时间", show: false },

        { label: "备注", show: true },
      ],
      // 是否显示详情
      showDetailVisible: false,
      // 数据详情
      detail: {},
      // 是否显示新增
      showAddVisible: false,
      // 新增数据
      addForm: {},
      // 是否显示修改
      showEditVisible: false,
      // 修改数据
      editForm: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 显示自定义列
    showSetColumn() {
      this.$refs.columns.dialogVisible = true;
    },
    filterTag(value, row) {
      return row.sort === value;
    },
    getList() {
      this.$http.post("/v1/api/topics/page", this.query).then((res) => {
        if (res.data.code == 0) {
          this.list = res.data.data.list;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize;
      this.getList();
    },
    // 监听 page 改变的事件
    handleCurrentChange(page) {
      this.query.pageNum = page;
      this.getList();
    },
    // 显示详情
    showDetailDialog(row) {
      this.detail = row;
      this.showDetailVisible = true;
    },
    // 隐藏详情
    hideDetailDialog() {
      this.detail = {};
      this.showDetailVisible = false;
    },
    // 显示新增
    showAddDialog() {
      this.addForm = {
        description: "",
        name: "",
        sort: "",
        remarks: "",
      };
      this.showAddVisible = true;
    },
    // 隐藏新增
    hideAddDialog() {
      this.showAddVisible = false;
    },
    add() {
      this.$refs.addFormRef.validate((valid) => {
        if (valid) {
          this.$http.post("/v1/api/topics", this.addForm).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("新增成功");
              this.showAddVisible = false;
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("请填写必填项");
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("此操作将永久删除该通知, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("/v1/api/topics/" + id).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("删除成功");
            }
          });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    // 显示修改
    showEditDialog(row) {
      this.editForm = row;
      this.showEditVisible = true;
    },
    // 隐藏修改
    hideEditDialog() {
      this.showEditVisible = false;
    },
    // 修改
    edit() {
      this.$refs.editFormRef.validate((valid) => {
        if (valid) {
          this.$http.put("/v1/api/topics", this.editForm).then((res) => {
            if (res.data.code == 0) {
              this.getList();
              this.$message.success("修改成功");
              this.showEditVisible = false;
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("请填写必填项");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}
</style>
